package models

import (
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)"json:"username"`
	Password string `gorm:"type:varchar(225)"json:"password"`
}

type Userrolegxb struct {
	gorm.Model
	Uid int `gorm:"type:char(10)"json:"uid"`
	Rid int `gorm:"type:char(10)"json:"rid"`
}

type Role struct {
	gorm.Model
	Rename string `gorm:"type:varchar(30)"json:"rename"`
}

type Rolequangxb struct {
	gorm.Model
	Qid int `gorm:"type:char(10)"json:"qid"`
	Rid int `gorm:"type:char(10)"json:"rid"`
}
type Quan struct {
	gorm.Model
	Quantum string `gorm:"type:varchar(30)"json:"quantum"`
}

type Comes struct {
	gorm.Model
	Codename string `gorm:"type:varchar(30)"json:"codename"`
}

type Types struct {
	gorm.Model
	Typename string `gorm:"type:varchar(30)"json:"typename"`
}

type Orders struct {
	gorm.Model
	Orderliness string `gorm:"type:varchar(30)"json:"orderliness"`
	Cid         int    `gorm:"type:char(10)"json:"cid"`
	Tid         int    `gorm:"type:char(10)"json:"tid"`
}

func (*User) Dengs(username string, user *User) error {
	return DB.Where("username=?", username).First(&user).Error
}
func (*Rolequangxb) DataAdd(data *Rolequangxb) error {
	tx := DB.Begin()
	err := DB.Debug().Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (r *Role) Relist(uid int) error {
	err := DB.Table("roles").
		Joins("LEFT JOIN userrolegxbs on userrolegxbs.rid=roles.id").
		Where("userrolegxbs.uid=?", uid).
		Find(&r).Error
	return err
}
func (q *Quan) Quantise(rid int) error {
	err := DB.Table("quans").
		Joins("LEFT JOIN rolequangxbs on rolequangxbs.qid=quans.id").
		Where("rolequangxbs.rid=?", rid).
		Find(&q).Error
	return err
}
func (*Comes) ComesAdds(data *Comes) error {
	tx := DB.Begin()
	err := DB.Debug().Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (*Orders) OrdersAdds(data *Orders) error {
	tx := DB.Begin()
	err := DB.Debug().Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
